#include <stdio.h>

unsigned invert(unsigned x, unsigned p, unsigned n);

int main()
{
    printf("out = %u\n", invert(0b11111111, 1, 5));
    return 0;
}

unsigned invert(unsigned x, unsigned p, unsigned n)
{
    unsigned mask = ~(~0 << n) << p;
    return ~(mask & x) & mask | (~mask & x);
}